package middleware

import (
	"ginstudydemo.com/main/service"
	"github.com/gin-gonic/gin"
)

type IJwt interface {
	JWTAuth()
}
type Jwt struct{}

func NewJwt() *Jwt {
	return &Jwt{}
}

// CheckJWTHandler 验证解密token
func (j Jwt) JWTAuth() gin.HandlerFunc {
	return func(c *gin.Context) {

		//引入jwt实现登录后的会话记录,登录会话发生登录完成之后
		//header获取token
		token := c.Request.Header.Get("token")
		if token == "" {
			service.ReturnErr(c, "5000", "未携带token")
			c.Abort()
			return
		}
		//解析token
		claims, tokenErr := service.NewJwt().DecodeUserToken(token)
		if claims == nil || tokenErr != nil {
			service.ReturnErr(c, "5005", tokenErr)
			c.Abort()
			return
		} else {
			//context.Set("user", claims.Username)
			c.Next()

		}
	}
}
